package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.RecommendIncomeRecords;
import cn.stylefeng.guns.modular.ums.mapper.RecommendIncomeRecordsMapper;
import cn.stylefeng.guns.modular.ums.model.params.RecommendIncomeRecordsParam;
import cn.stylefeng.guns.modular.ums.model.result.RecommendIncomeRecordsResult;
import  cn.stylefeng.guns.modular.ums.service.RecommendIncomeRecordsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 教师推广收益 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2020-02-05
 */
@Service
public class RecommendIncomeRecordsServiceImpl extends ServiceImpl<RecommendIncomeRecordsMapper, RecommendIncomeRecords> implements RecommendIncomeRecordsService {

    @Override
    public void add(RecommendIncomeRecordsParam param){
        RecommendIncomeRecords entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(RecommendIncomeRecordsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(RecommendIncomeRecordsParam param){
        RecommendIncomeRecords oldEntity = getOldEntity(param);
        RecommendIncomeRecords newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public RecommendIncomeRecordsResult findBySpec(RecommendIncomeRecordsParam param){
        return null;
    }

    @Override
    public List<RecommendIncomeRecordsResult> findListBySpec(RecommendIncomeRecordsParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(RecommendIncomeRecordsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(RecommendIncomeRecordsParam param){
        return param.getRecordsId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private RecommendIncomeRecords getOldEntity(RecommendIncomeRecordsParam param) {
        return this.getById(getKey(param));
    }

    private RecommendIncomeRecords getEntity(RecommendIncomeRecordsParam param) {
        RecommendIncomeRecords entity = new RecommendIncomeRecords();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
